#include "CEditor.h"

int CEditor::not_finished = 0; // Etat des évènements en cours

CEditor::CEditor()
{
    int i;

    i = 0;
    while (i < (80 * 60))
    {
        (this->table)[i] = NULL;
        i += 1;
    }
    this->working_map = NULL;
    this->selected_type = MCASE_BUILDING;
}

CEditor::~CEditor()
{
    this->delete_map();
}

// Ferme toutes les boîtes de dialogue
void CEditor::close_all_gates()
{
    int i;

    i = this->opened_gates.size();
    while (i > 0)
    {
        i -= 1;
        this->opened_gates.at(i)->cancel();
    }
}

// Crée une nouvelle carte
void CEditor::new_map()
{
    int i;

    i = 0;
    while (i < (80 * 60))
    {
        if ((this->table)[i] != NULL)
            delete (this->table)[i];
        (this->table)[i] = NULL;
        i += 1;
    }
    if (this->working_map != NULL)
        delete this->working_map;
    this->working_map = new MMap();
}

// Détruit une carte
void CEditor::delete_map()
{
    int i;

    i = 0;
    while (i < (80 * 60))
    {
        if ((this->table)[i] != NULL)
        {
            delete (this->table)[i];
            (this->table)[i] = NULL;
        }
        i += 1;
    }
    if (this->working_map != NULL)
        delete this->working_map;
}

// Charge une carte depuis un fichier
void CEditor::load_map(QString path)
{
    std::string temp_path;
    MDataMap *data_map;

    if ((temp_path = path.toStdString()) == "")
        return;
    data_map = new MDataMap();

    std::ifstream ifs(temp_path.c_str(), std::ios::binary);
    ifs.read((char *)(data_map), sizeof(MDataMap));
    this->delete_map();
    this->working_map = data_map->get_map();
}

// Sauvegarde une carte
void CEditor::save_map(QString path)
{
    std::string temp_path;
    MDataMap *data_map;

    if ((temp_path = path.toStdString()) == "")
        return;
    data_map = new MDataMap(this->working_map);

    std::ofstream ofs(temp_path.c_str(), std::ios::binary);
    ofs.write((char *)(data_map), sizeof(MDataMap));
}

// Vérifie que la position n'est pas déjà en train d'être éditée
int CEditor::position_event(QMouseEvent *event)
{
    int i;

    i = this->editor_positions.size();
    while (i > 0)
    {
        i -= 1;
        if (this->editor_positions.at(i)->x == (event->x() / 10) && this->editor_positions.at(i)->y == (event->y() / 10))
            return (1);
    }
    return (0);
}

// Gère la création/destruction d'objets
void CEditor::handle_mouse_release(QLabel *label, QMouseEvent *event)
{
    int pos;
    QPixmap *pixmap;
    VGate *gate_window;
    struct CEditorPosition *temp_position;

    CEditor::not_finished += 1;
    gate_window = NULL;
    if ((event->button() != Qt::LeftButton && event->button() != Qt::RightButton) || this->position_event(event))
    {
        delete label;
        CEditor::not_finished -= 1;
        return;
    }
    temp_position = new struct CEditorPosition;
    temp_position->x = event->x() / 10;
    temp_position->y = event->y() / 10;
    this->editor_positions.append(temp_position);
    pos = ((event->y() / 10) * 80) + (event->x() / 10);
    if ((this->working_map->table)[pos] != NULL)
        delete (this->working_map->table)[pos];
    if ((this->table)[pos] != NULL)
        delete (this->table)[pos];
    (this->working_map->table)[pos] = NULL;
    (this->table)[pos] = NULL;
    (this->table)[pos] = label;
    if (this->selected_type == MCASE_BUILDING && event->button() == Qt::LeftButton)
    {
        (this->working_map->table)[pos] = new MBuilding(event->x() / 10, event->y() / 10);
        pixmap = new QPixmap(MBuilding::image);
    }
    else if (this->selected_type == MCASE_GATE && event->button() == Qt::LeftButton)
    {
        gate_window = new VGate();
        this->opened_gates.append(gate_window);
        gate_window->setVisible(true);
        gate_window->exec();
        if (gate_window->ip == "" || gate_window->port == (quint16)(0))
        {
            this->opened_gates.removeOne(gate_window);
            delete gate_window;
            delete label;
            (this->table)[pos] = NULL;
            this->editor_positions.removeOne(temp_position);
            CEditor::not_finished -= 1;
            return;
        }
        (this->working_map->table)[pos] = new MGate(event->x() / 10, event->y() / 10, gate_window->ip, gate_window->port);
        this->opened_gates.removeOne(gate_window);
        delete gate_window;
        pixmap = new QPixmap(MGate::image);
    }
    else
    {
        delete label;
        (this->table)[pos] = NULL;
        this->editor_positions.removeOne(temp_position);
        CEditor::not_finished -= 1;
        return;
    }
    label->setPixmap(*pixmap);
    label->move((event->x() / 10) * 10, (event->y() / 10) * 10);
    label->show();
    delete pixmap;
    this->editor_positions.removeOne(temp_position);
    CEditor::not_finished -= 1;
}
